package com.newa.actions;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import com.newa.beans.Orderu;
import com.newa.dao.DAOOrder;
import com.newa.utils.HibUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.LocalizedTextUtil;

public class addOrder extends ActionSupport {
	private Logger LOG = Logger.getLogger(addOrder.class);
	private Orderu order;
	private File info;
	private String infoFileName;
	private String infoContentType;

	@Override
	public String execute() throws Exception {
		Session session = HibUtil.getSessionFactory().openSession();
		DAOOrder daoo = new DAOOrder(session);
		try {
			if(info!=null){
				String path = (ServletActionContext.getServletContext()
						.getRealPath("/pages/orderFiles"));
				System.out.println(path);
				order.setPFILE(path+"/"+getInfoFileName());
				File logoToCreate = new File(path, getInfoFileName());
				FileUtils.copyFile(info, logoToCreate);
			}
			daoo.createOrder(order);
			
		} catch (Exception e) {
			LOG.warn(e);
			throw new RuntimeException(e);
		} finally {
			daoo.closeSession().close();
		}
		return "success";
	}

	public Orderu getOrder() {
		return order;
	}

	public void setOrder(Orderu order) {
		this.order = order;
	}

	public File getInfo() {
		return info;
	}

	public void setInfo(File info) {
		this.info = info;
	}

	public String getInfoFileName() {
		return infoFileName;
	}

	public void setInfoFileName(String infoFileName) {
		this.infoFileName = infoFileName;
	}

	public String getInfoContentType() {
		return infoContentType;
	}

	public void setInfoContentType(String infoContentType) {
		this.infoContentType = infoContentType;
	}

}
